<?php


namespace app\common\model;

use traits\model\SoftDelete;
class Clue extends Base
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $autoWriteTimestamp = true;

    /**
     * 线索账号准入城市
     * @param $account_id string 投放账户id
     * @param $city_str string 城市id字符 6-79
     * @return bool
     */
    public function allowAccountCity($account_id, $city_str = "")
    {
        if (empty($city_str)) { // 没有线索城市，默认准入
            return true;
        }
        $allow_info = \db('account_city_in')->where(['acc_id' => $account_id])->select();
        if (empty($allow_info)) { // 账号没有设置 则默认准入
            return true;
        }

        $is_allow = false;
        foreach ($allow_info as $v) {
            // 城市 是否启用
            if ($v['city'] == $city_str && $v['status'] == 1) {
                // 当天账号城市的线索数
                $today_clue_num = \db('clue')->where(['account_id' => $account_id, 'city' => $city_str])->whereTime('create_time', 'd')->count();
                if ($today_clue_num < $v['accept_limit_num']) { //  每日线索接收数是否超出
                    $is_allow = true;
                }
            }
        }
        return $is_allow;
    }

    /**
     * 判断进线的线索是否重复
     * 当天 手机号、账户 是否有重复线索
     * 返回 bool 重复为TRUE，不重复为FALSE
     * @param $phone string 手机号
     * @param $adv_id string 账户id
     * @return bool
     */
    public function isRepeatClue($phone, $adv_id)
    {
        $isDayPhone = \db('clue')->where(['phone' => $phone, 'adv_id' => $adv_id])->whereTime('create_time', 'd')->find();
        return !empty($isDayPhone);
    }
}